package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.Advposnums;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.entity.Article;
import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.mapper.AdvposMapper;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class AdvposServiceImpl extends ServiceImpl<AdvposMapper, Advpos> implements AdvposService {
    @Autowired
    private AdvimgService advimgService;

    @Override
    public HashMap<String, Object> getPageHashMap(String advposdesc, String advposname, Integer page, Integer limit) {
        Page<Advpos> advposPage = new Page<>(page, limit);
        LambdaQueryWrapper<Advpos> lqw = new LambdaQueryWrapper<>();
        lqw.like(advposdesc != null, Advpos::getAdvposdesc, advposdesc)
                .like(advposname != null, Advpos::getAdvposname, advposname);
        this.page(advposPage, lqw);
        List<Advpos> records = advposPage.getRecords();
        List<Advposnums> advposnums = advimgService.getAdvposnums();
        records.forEach(iter -> {
            advposnums.forEach(item->{
                if (iter.getId().equals(item.getAdvimgpos())){
                    iter.setAdvposnum(item.getAdvposnum());
                }
            });
        });
        Integer count = this.count(lqw);
        HashMap<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("currentPage", page);
        map.put("pageSize", limit);
        map.put("totalPages", advposPage.getTotal());
        map.put("data", records);
        return map;
    }
}
